#include <stdio.h>
#include <stdlib.h>
// #include <math.h>
#include <string.h>
#include <windows.h>
// getch
#include <conio.h>

#include "HeadFiles/TotalProfit.h"
#include "HeadFiles/ChainsH/RecordChain.h"
#include "HeadFiles/ChainsH/ClientChain.h"
#include "HeadFiles/ChainsH/CommodityChain.h"
#include "HeadFiles/ChainsH/CartChain.h"
#include "HeadFiles/ChainsH/ChainOpt.h"

#include "HeadFiles/PagesH/RecordPage.h"
#include "HeadFiles/PagesH/ClientPage.h"
#include "HeadFiles/PagesH/WelcomePage.h"
#include "HeadFiles/PagesH/LoginPage.h"
#include "HeadFiles/PagesH/CommodityPage.h"


#include "HeadFiles/MD5.h"

#include "HeadFiles/InputCheck.h"
#include "HeadFiles/StrSimilar.h"
#include "HeadFiles/PagesH/GeneralPage.h"
#include "HeadFiles/PagesH/CostumerViewCommoditiesPage.h"
#include "HeadFiles/PagesH/CartDetailPage.h"

struct RecordNode * recordHead = NULL, * recordTail = NULL;
struct ClientNode * clientHead = NULL, * clientTail = NULL;
struct CommodityNode * commodityHead = NULL, * commodityTail = NULL;
struct CartNode * cartHead = NULL, * cartTail = NULL;

struct RecordNode * recordDisplayPtr = NULL;
struct ClientNode * clientDisplayPtr = NULL;
struct CommodityNode * commodityDisplayPtr = NULL;
struct CartNode * cartDisplayPtr = NULL;

void init();

void displayThisRecordForClient(struct RecordNode * this);

char yourName[STR_MAX_LEN];

int main()
{

    backupAll(); // 打开程序第一件事是备份

    LoginPage:// 退出登录之后也会来到这里

    init();// 所有指针清空

    buildClientChain(&clientHead, &clientTail);// 因为涉及到注册，所以提前build

    memset(yourName, 0 ,sizeof yourName);
    int auth = LoginPage(yourName);
    if (auth == 2) // admin
    {
        puts("Now Building Record Chain..");
        buildRecordChain(&recordHead, &recordTail);
        puts("Record Chain Build Complete!");

        //将Record与Commodity连接

        puts("Now Building Commodity Chain..");
        buildCommodityChain(&commodityHead, &commodityTail);
        puts("Commodity Chain Build Complete!");
        system("pause");

        // 由新到旧展示，则第一个展示的是链表尾
        recordDisplayPtr = recordTail;
        clientDisplayPtr = clientTail;
        commodityDisplayPtr = commodityTail;
        // 登录完成后进入WelcomePage

        Welcome_Admin_Page:
        WelcomeAdminPage();
        // 1-查询现有销售记录  2-查询现有货物列表  3-查询现有客户列表  4-查看总览报表   0-退出程序
        char opt1 = getche();
        while (!(opt1 == '1' || opt1 == '2' || opt1 == '3' || opt1 == '4' || opt1 == '0')) {
            puts("Illegal input detected! Try again: ");
            opt1 = getche();
        }

        // opt1已经合法
        if (opt1 == '1')             // 销售记录页面
        {
            Record_Page:
            puts("To RecordPage..");
            int toGo = RecordPage();
            rebuildRecordFile(recordHead);
            if (toGo == 1)       // 1保留当前页面，0回到主界面
                goto Record_Page;
            else if (toGo == 0)
                goto Welcome_Admin_Page;
            else // 未知的返回值?
            {
                puts("Unexpected return val from RecordPage()!");
                puts("Please contact dev teams for help!");
                puts("Now redirect to WelcomeAdminPage.....");
                system("pause");
                goto Welcome_Admin_Page;
            }
        } else if (opt1 == '2') {      // 商品页面
            Commodity_Page:
            puts("");
            int toGo = CommodityPage();
            if(toGo == 1)
                goto Commodity_Page;
            else if(toGo == 0)
                goto Welcome_Admin_Page;
        } else if (opt1 == '3') {      // 客户页面
            Client_Page:
            puts("");
            int toGo = ClientPage();
            if (toGo == 1)
                goto Client_Page;
            else if (toGo == 0)
                goto Welcome_Admin_Page;
        } else if (opt1 == '4') {      //总览页面
            int toGO = OverPage();
            if (toGO == 0)
                goto Welcome_Admin_Page;
        } else {
            // 退出登录
            puts("Now exiting....");
            releaseAll();
            system("pause");
            goto LoginPage;
        }
    }
        //Costumer
    else if (auth == 1)
    {
        struct ClientNode * nowClient = getClientPtrFromName(yourName);
        puts("Now Building Record Chain..");

        buildRecordChain(&recordHead, &recordTail);
        puts("Record Chain Build Complete!");

        puts("Now Building Commodity Chain..");
        buildCommodityChain(&commodityHead, &commodityTail);
        puts("Commodity Chain Build Complete!");

        Welcome_Costumer_Page:
        WelcomeCostumerPage();
        printf("Your Balance: %.2f\n", nowClient->balance);
        printf("Your Lv: %d\n", nowClient->lv);
        char opt3 = getche();
        while (!(opt3 == '1' || opt3 == '2' || opt3 == '3' || opt3 == '0'))
        {
            puts("Illegal input detected! Try again: ");
            opt3 = getche();
        }
        if (opt3 == '1')
        {
            // 查看商品清单
            Costumer_View_Commodities_Page:
            system("cls");
            int toGO = costumerViewCommodities(nowClient);
            goto Welcome_Costumer_Page;
        }else if (opt3 == '2'){
            // 查看购买历史
            struct RecordNode * searchPtr = recordHead;
            while(searchPtr != NULL)
            {
                if(strcmp(searchPtr->clientName, yourName) == 0)
                    displayThisRecordForClient(searchPtr);
                searchPtr = searchPtr->nextRecord;
            }
            system("pause");
            goto Welcome_Costumer_Page;
        }else if (opt3 == '3'){
            // 查看购物车
            int toGo = cartDetailPage(nowClient);
            system("pause");
            goto Welcome_Costumer_Page;
        }else{
            // 退出登录
            goto LoginPage;
        }
    }

    puts("Exit");
    system("pause");
    return 0;
}


void init()
{
    recordHead = NULL, recordTail = NULL;
    clientHead = NULL, clientTail = NULL;
    commodityHead = NULL, commodityTail = NULL;
    cartHead = NULL, cartTail = NULL;

    recordDisplayPtr = NULL;
    clientDisplayPtr = NULL;
    commodityDisplayPtr = NULL;
    cartDisplayPtr = NULL;
}

void displayThisRecordForClient(struct RecordNode * this)
{
    puts(SPLIT_STR);
    printf("Commodity Name: %s\n", this->commodityName);
    printf("Unit Price: %.2f\n", this->soldPrice);
    printf("Unit Count: %d\n", this->unitCount);
    printf("Total: %.2f\n", this->soldPrice * this->unitCount);
    printf("History Level: %d\n", this->historyLv);
    puts(SPLIT_STR);
}